<template>
	<view class="content">
		<view class="avatorWrapper">
			<view class="avator">
				<image class="img" src="/static/logo.png" mode="widthFix"></image>
			</view>
		</view>
		<view class="form">
			<view class="inputWrapper">
				<input class="input" type="text" v-model="uname" value="" placeholder="请输入用户名" />
			</view>
			<view class="inputWrapper">
				<input class="input" type="password" v-model="passwd" value="" placeholder="请输入密码" />
			</view>
			<view class="loginBtn">
				<text class="btnValue" @tap="onLogin">登录</text>
			</view>
			<view class="forgotBtn">
				<text>找回密码</text>
			</view>
		</view>
	</view>
</template>

<script>
	import {
		mapState,
	    mapMutations
	} from 'vuex';
	export default {
		data() {
			return {
				uname: '',
				passwd: '',
				rememberPsw: true
			}
		},
		mounted() {
			let that = this;
			//缓存的账号
			const HCuname = this.$store.state.userInfo.username;
			//缓存的密码
			const HCpasswd = ''
			//         console.log(HCpassw+"缓存的密码")
			//         console.log(HCuname)
			//有缓存就赋值给文本没有就清空
			if (HCuname) {
				that.uname = HCuname;
			} else {
				that.uname = '';
				that.passwd = '';
			}
		},
		methods: {
			...mapMutations(['login']),
			async onLogin() {
				var that = this;
				if (this.uname.length <= 0 || this.passwd.length <= 0) {
					uni.showToast({
						title: '账号或密码不能为空',
						icon: 'none'
					});
					return;
				} else {
					const user = await this.$myRequest({
						url : getApp().globalData.baseUrl +  '/api.system/login',
						method: 'POST',
						data: {
							username : this.uname,
							password : this.passwd
						}
					})
					if (user.data.code === 1) {
						console.log('登录后的数据:',user.data)
						this.login(user.data.data);
						uni.reLaunch({
							url: '../index/index'
						})
					} else {
						uni.showToast({
							title: '用户名或密码错误',
							icon: 'none'
						});
					}

				}
			},
		}
	}
</script>

<style lang="scss">
	.content {
		background: #377EB4;
		width: 100vw;
		height: 100vh;
	}

	.avatorWrapper {
		height: 30vh;
		width: 100vw;
		display: flex;
		justify-content: center;
		align-items: flex-end;
	}

	.avator {
		width: 200upx;
		height: 200upx;
		overflow: hidden;
	}

	.avator .img {
		width: 100%
	}

	.form {
		padding: 0 100upx;
		margin-top: 80px;
	}

	.inputWrapper {
		width: 100%;
		height: 80upx;
		background: white;
		border-radius: 20px;
		box-sizing: border-box;
		padding: 0 20px;
		margin-top: 25px;
	}

	.inputWrapper .input {
		width: 100%;
		height: 100%;
		text-align: center;
		font-size: 15px;
	}

	.loginBtn {
		width: 100%;
		height: 80upx;
		background: #77B307;
		border-radius: 50upx;
		margin-top: 50px;
		display: flex;
		justify-content: center;
		align-items: center;

	}

	.loginBtn .btnValue {
		color: white;
	}

	.forgotBtn {
		text-align: center;
		color: #EAF6F9;
		font-size: 15px;
		margin-top: 20px;
	}
</style>
